/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 */

import { HvigorConfigLoader } from '../../../common/util/hvigor-config-loader.js';
import { AnalyzeMode, AnalyzeModeMap, coreParameter, OldAnalyzeModeMap } from '../../internal/data/global-core-parameters.js';
import { globalData } from '../../internal/data/global-data.js';
import { HvigorLogger } from '../../log/hvigor-log.js';
import { HvigorConfig } from '../../util/hvigor-config-reader.js';
import { HvigorCliOptions } from '../../util/options/hvigor-cli-options.js';

const _log: HvigorLogger = HvigorLogger.getLogger('process-build-analyze-mode');

/**
 * 处理历史变更已经deprecated的cli命令行&构建配置
 *
 * @param {HvigorCliOptions} hvigorCliOptions
 */
export function processLegacyConfig(hvigorCliOptions?: HvigorCliOptions) {
  processAnalyzeMode(hvigorCliOptions);
  processLegacyCliAnalyzeMode(hvigorCliOptions);
  processLegacyConfigAnalyzeMode(hvigorCliOptions);
}

/**
 * 针对命令行中的多个配置进行过滤和合并处理
 *
 * @param {HvigorCliOptions} hvigorCliOptions
 */
function processAnalyzeMode(hvigorCliOptions?: HvigorCliOptions) {
  const analyzeCommandLineStr = '--analyze=';
  if (!hvigorCliOptions) {
    return;
  }

  const analyzeModeList = hvigorCliOptions._.filter((x) => x.startsWith(analyzeCommandLineStr));
  if (analyzeModeList && analyzeModeList.length) {
    hvigorCliOptions.analyze = analyzeModeList[analyzeModeList.length - 1].substring(analyzeCommandLineStr.length);
    hvigorCliOptions._ = hvigorCliOptions._.filter((x) => !x.startsWith(analyzeCommandLineStr));
  }
}

/**
 * 处理hvigor-config.json配置文件中,使用deprecated的key-value参数,需要给出告警提示
 * eg."analyze": "default"
 */
function processLegacyConfigAnalyzeMode(hvigorCliOptions?: HvigorCliOptions) {
  const hvigorConfig: HvigorConfig | undefined = HvigorConfigLoader.getInstance().getHvigorConfig();
  if (hvigorConfig?.execution?.analyze === undefined) {
    return;
  }
  const newMode = OldAnalyzeModeMap.get(hvigorConfig.execution.analyze);
  if (newMode) {
    _log.warn(`Build analysis mode '${hvigorConfig.execution.analyze}' is deprecated,` + ` please use '${newMode}' instead.`);
  }
  // 修改analyze字段生效的顺序，命令行参数/settings > hvigor-config
  if (hvigorCliOptions?.analyze || hvigorCliOptions?.analyze === false) {
    return;
  }
  coreParameter.startParams.analyze = AnalyzeModeMap.get(hvigorConfig.execution.analyze) ?? AnalyzeMode.NORMAL;
}

/**
 * 处理命令行场景下,使用deprecated的命令行参数,需要给出告警提示
 * eg.--verbose-analyze ---> --analyze=advanced
 *
 * @param {HvigorCliOptions} hvigorCliOptions
 */
function processLegacyCliAnalyzeMode(hvigorCliOptions?: HvigorCliOptions) {
  const opts = hvigorCliOptions ?? globalData.cliOpts;
  if (opts.verboseAnalyze) {
    opts.analyze = 'advance';
    coreParameter.startParams.analyze = AnalyzeMode.ADVANCED;
    _log.warn("Command line '--verbose-analyze' is deprecated, please use '--analyze=advanced' instead.");
  }
  if (opts.analyze === undefined) {
    return;
  }

  if (typeof opts.analyze === 'boolean') {
    useOldAnalyzeMode(opts.analyze);
  } else {
    useNewAnalyzeMode(opts.analyze);
  }
}

function useOldAnalyzeMode(analyze: boolean) {
  if (analyze) {
    coreParameter.startParams.analyze = AnalyzeMode.NORMAL;
    _log.warn("Command line '--analyze' is deprecated, please use '--analyze=normal' instead.");
  } else {
    coreParameter.startParams.analyze = AnalyzeMode.FALSE;
    _log.warn("Command line '--no-analyze' is deprecated, please use '--analyze=false' instead.");
  }
}

function useNewAnalyzeMode(analyze: string) {
  const newMode = AnalyzeModeMap.get(analyze);
  if (newMode === undefined) {
    _log.warn(`Build analysis mode: ${analyze} is not available,` + " value should be one of: 'normal', 'advanced', 'ultrafine' and 'false'.");
  } else {
    coreParameter.startParams.analyze = newMode;
  }
}
